<?php


namespace app\applet\model;


use think\Db;

class GoodsShare
{

    /**
     * 干货分享---轮播图
     */
    public function banner(){
        $data=Db::name('advert')
            ->field('advert_pic,jump_type,jump_id')
            ->where(['type'=>3,'status'=>1])
            ->order('id desc')
            ->limit(5)
            ->select();
        return $data;
    }


    /**
     * 干货分享  热门（返利）课程  精选（裂变）课程
     */
    public function hotCourse(){

        $field='g.goods_id,g.cover_pic,g.g_name,gi.intro,gs.price';
        //精选课程
        $data=Db::name('goods')->alias('g')
            ->field($field)
            ->join('goods_info gi','gi.goods_id=g.goods_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where(['g.source_type'=>4,'g.g_type'=>3])
            ->limit(2)
            ->select();
        //热门课程
        $info=Db::name('goods')->alias('g')
            ->field($field)
            ->join('goods_class gc','gc.gc_id=g.gc_id')
            ->join('goods_info gi','gi.goods_id=g.goods_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where(['g.source_type'=>4,'g.g_type'=>2])
            ->limit(2)
            ->select();
        if(!empty($data)){
            foreach ($data as $k=>&$v){
                //观看人数
                $count=Db::name('course_viewers_record')
                    ->where('goods_id',$v['goods_id'])
                    ->count();
                $v['viewers']=$count;
            }
        }

        if(!empty($info)){
            foreach ($info as $k=>&$v){
                //观看人数
                $count=Db::name('course_viewers_record')
                    ->where('goods_id',$v['goods_id'])
                    ->count();
                $v['viewers']=$count;
            }
        }

        $list['hotData']=$info;
        $list['choiceData']=$data;

        return $list;
    }

    /**
     * 干货分享 我的课程 （我购买或者观看过的课程）
     */
    public function myCourse($member_id){

        $where=[];
        //查看用户购买的课程
        $goods_ids=$this->viewCourse($member_id);

        if(!empty($goods_ids)){
            $where['g.goods_id']=['in',$goods_ids];
        }

        $field='g.goods_id,g.cover_pic,g.g_name,g.g_type';

        $data=Db::name('goods')->alias('g')
            ->field($field)
            ->where(['g.source_type'=>4])
            ->where($where)
            ->limit(3)
            ->select();

        return $data;
    }

    /**
     *  查询用户购买或浏览课程
     */
    public function viewCourse($member_id){

        $order=Db::name('course_buy_record')
            ->field('goods_id')
            ->where(['member_id'=>$member_id])
            ->select();

        $order2=Db::name('course_viewers_record')
            ->field('goods_id')
            ->where(['member_id'=>$member_id])
            ->select();


        $m_order=[];
        if(!empty($order)){
            foreach ($order as $k=>$v){
                $m_order[$k]=$v['goods_id'];
            }
        }
        $m_order2=[];
        if(!empty($order2)){
            foreach ($order2 as $k=>$v){
                $m_order2[$k]=$v['goods_id'];
            }
        }
        $test=array_merge($m_order,$m_order2);
        $test2=array_unique($test);
        $test3=array_values($test2);

        return $test3;
    }

    /**
     * 干货分享  列表  type  1=热门(裂变) 2=精选（返利）
     */
    public function goodsList($page,$page_size,$type){
        $where=[];
        if($type==1){
            $where['g.g_type']=2;
        }else{
            $where['g.g_type']=3;
        }
        $field='g.goods_id,g.cover_pic,g.g_name,g.g_type,gi.intro,gs.price';

        $data=Db::name('goods')->alias('g')
            ->field($field)
            ->join('goods_info gi','gi.goods_id=g.goods_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where(['g.source_type'=>4])
            ->where($where)
            ->page($page,$page_size)
            ->select();

        if(!empty($data)){
            foreach ($data as $k=>&$v){
                //观看人数
                $count=Db::name('course_viewers_record')
                    ->where('goods_id',$v['goods_id'])
                    ->count();
                $v['viewers']=$count;
            }
        }

        $total_num=Db::name('goods')->alias('g')
            ->join('goods_info gi','gi.goods_id=g.goods_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where(['g.source_type'=>4])
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 干货分享 商品详情  type  1=热门(裂变) 2=精选（返利）
     */
    public function goodsDetail($member_id,$goods_id){
        $field='g.goods_id,g.g_name,g.g_type,gi.video_path,gi.product_details_image,gs.price,gs.coin_price,
        gs.gold_coin_num,gs.proportion,gs.gs_id,gi.product_details_image,
        gi.copper_money,gi.copper_pop,gi.silver_money,gi.silver_pop,gi.gold_money,gi.gold_pop,gi.free_time';

        $goods_share=Db::name('system_config')
            ->where(['field_name'=>'rebate_mall_set'])
            ->value('field_value');
        $share=json_decode($goods_share,true);

       $data=Db::name('goods')->alias('g')
           ->field($field)
           ->join('goods_info gi','gi.goods_id=g.goods_id')
           ->join('goods_spec gs','gs.goods_id=g.goods_id')
           ->where(['g.goods_id'=>$goods_id,'g.source_type'=>4,'g.status'=>1,'g.is_delete'=>1])
           ->find();
       //观看人数
        $count=Db::name('course_viewers_record')
            ->where('goods_id',$goods_id)
            ->count();
        $data['viewers']=$count;
        //是否购买
        $buy=Db::name('course_buy_record')
            ->where(['goods_id'=>$goods_id,'member_id'=>$member_id])
            ->count();
        if($buy>0){
            $data['is_buy']=1;//已购买
        }else{
            $data['is_buy']=2;//未购买
        }
        //是否收藏
        $count2 = Db::name("collection")
            ->where(['class_type'=>14,'object_id'=>$goods_id,'member_id'=>$member_id])
            ->count();
        if($count2>0){
            $data['is_collection']=1;//收藏
        }else{
            $data['is_collection']=2;//未收藏
        }

        //买家积分奖
        $data['buyer']=$share['rebate_buyer_pop'];
        $data['direct']=$share['share_direct_price'];
        $data['inter']=$share['share_inter_price'];

        $data['video_path'] = json_decode($data['video_path'],true);
        $data['product_details_image'] = json_decode($data['product_details_image'],true);

        return $data;

    }

    /**
     * 干货分享 商品列表
     */
    public function myCourseList($member_id,$page,$page_size){

        $where=[];
        //查看用户购买的课程
        $goods_ids=$this->viewCourse($member_id);

        if(!empty($goods_ids)){
            $where['g.goods_id']=['in',$goods_ids];
        }

        $field='g.goods_id,g.cover_pic,g.g_name,gi.intro,gs.price,g.g_type';

        $data=Db::name('goods')->alias('g')
            ->field($field)
            ->join('goods_info gi','gi.goods_id=g.goods_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where(['g.source_type'=>4,'g.status'=>1,'g.is_delete'=>1])
            ->where($where)
            ->page($page,$page_size)
            ->select();

        if(!empty($data)){
            foreach ($data as $k=>&$v){
                //观看人数
                $count=Db::name('course_viewers_record')
                    ->where('goods_id',$v['goods_id'])
                    ->count();
                $v['viewers']=$count;
            }
        }

        $total_num=Db::name('goods')->alias('g')
            ->join('goods_info gi','gi.goods_id=g.goods_id')
            ->join('goods_spec gs','gs.goods_id=g.goods_id')
            ->where(['g.source_type'=>4,'g.status'=>1,'g.is_delete'=>1])
            ->where($where)
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;

    }

    /**
     * 干货分享  免费试看 写入观看记录
     */
    public function videoSee($goods_id,$member_id){

        $data=Db::name('course_viewers_record')
            ->where(['member_id'=>$member_id,'goods_id'=>$goods_id])
            ->find();
        if(empty($data)){
            Db::name('course_viewers_record')
                ->insert(['goods_id'=>$goods_id,'member_id'=>$member_id,'create_time'=>time()]);
        }

        return true;
    }
}